﻿using System;
using System.Windows.Forms;
using CloudShot.Core.Types;
using CloudShot.Interfaces;
using CloudShot.Properties;

namespace CloudShot.Services
{
  internal class NotificationService : INotificationService
  {
    private const int NotificationShowTime = 2000; //milliseconds

    private readonly NotifyIcon _notifyIcon;

    private Action _clickAction;

    public NotificationService(NotifyIcon notifyIcon)
    {
      _notifyIcon = notifyIcon;
      _notifyIcon.BalloonTipClicked += NotifyIconOnBalloonTipClicked;
    }

    public void ShowInfo(string title, string message)
    {
      ShowNotification(title, message, ToolTipIcon.Info);      
    }

    public void ShowInfo(string title, string message, Action notificationClickAction)
    {
      ShowNotification(title, message, ToolTipIcon.Info, notificationClickAction);
    }

    public void ShowInfo(ShotInfo shotInfo, Action notificationClickAction)
    {
      ShowNotification(Resources.Notifications_ShotSavedTitle, shotInfo.Path, ToolTipIcon.Info, notificationClickAction);
    }

    public void ShowWarning(string title, string message)
    {
      ShowNotification(title, message, ToolTipIcon.Warning);
    }

    public void ShowError(string title, string message)
    {
      ShowNotification(title, message, ToolTipIcon.Error);
    }

    private void ShowNotification(string title, string message, ToolTipIcon icon, Action clickAction = null)
    {
      _clickAction = clickAction;

      _notifyIcon.Visible = false;
      _notifyIcon.Visible = true;
      _notifyIcon.ShowBalloonTip(NotificationShowTime, title, message, icon);
    }

    private void NotifyIconOnBalloonTipClicked(object sender, EventArgs eventArgs)
    {
      if (_clickAction != null)
      {
        _clickAction();
        _clickAction = null;
      }
    }
  }
}